package com.cn.jinl.controller;


import com.cn.jinl.api.ICheckLineService;
import com.cn.jinl.domain.CheckLine;
import com.cn.jinl.domain.ChenxingMachine;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/checkLine")
public class CheckLineController {

    @Autowired
    private ICheckLineService checkLineService;

    @RequestMapping("/getHistoryCheckLineLists/{frontToken}")
    @ResponseBody
    public JsonResponse<List<CheckLine>> getHistoryCheckLineLists(@RequestBody QueryForm queryForm, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<CheckLine>> response = new JsonResponse<List<CheckLine>>();
        List<CheckLine> list  = checkLineService.getHistoryCheckLineLists(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/getCheckLineListMap/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getCheckLineListMap(QueryForm queryForm, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object> maps  = checkLineService.getCheckLineListMap(queryForm);
        response.setResult(maps);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/exportCheckLineList/{frontToken}")
    @ResponseBody
    public JsonResponse<String> exportCheckLineList(@RequestBody QueryForm queryForm, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = checkLineService.exportCheckLineList(queryForm);
        response.setResult(result);
        response.setSuccess(StringUtils.equals("设备查询异常",result)?false:true);
        response.setMessage(result);
        return response;
    }

    //查询线检
    @RequestMapping("/api/getCheckLineListMap")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getCheckLineListMap(@Param("deviceCode") String deviceCode,@Param("tabType") String tabType) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object> maps  = checkLineService.getCheckLineListMap(deviceCode,tabType);
        String msgError = (String) maps.get("error");
        if(StringUtils.isNotEmpty(msgError)){
            response.setSuccess(false);
            response.setMessage(msgError);
        }else {
            response.setResult(maps);
            response.setSuccess(true);
        }
        return  response;
    }

    //线检
    @ResponseBody
    @RequestMapping("/api/addOrUpdateCheckLine")
    public JsonResponse<String> addOrUpdateCheckLine(CheckLine checkLine) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = checkLineService.addOrUpdateCheckLine(checkLine);
        return CommonUtil.getResponseData(response,result);
    }

    //查询巡检动态
    @RequestMapping("/api/getAPICheckLineListMap")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getAPICheckLineListMap(QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object> maps  = checkLineService.getAPICheckLineListMap(queryForm);
        response.setResult(maps);
        response.setSuccess(true);
        return  response;
    }

    @ResponseBody
    @RequestMapping("/api/addOrUpdateTestCheckLine")
    public JsonResponse<String> addOrUpdateTestCheckLine() throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        CheckLine checkLine = new CheckLine();
        checkLine.setDeviceCode("L01");
        checkLine.setUserName("cxl");
        checkLine.setPlanName("aa");
        checkLine.setGradeValue("12*33");
        checkLine.setTabType("1");
        checkLine.setValue1("1");
        checkLine.setValue2("2");
        checkLine.setValue3("3");
        checkLine.setValue4("4");
        checkLine.setValue5("5");
        checkLine.setValue6("6");
        checkLine.setValue7("7");
        String result = checkLineService.addOrUpdateCheckLine(checkLine);
        return CommonUtil.getResponseData(response,result);
    }

    //质量分析
    @RequestMapping("/getQualityAnalysisList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Map<Object, Object>>> getQualityAnalysisList(@RequestBody QueryForm queryForm, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<Map<Object, Object>>> response = new JsonResponse<List<Map<Object, Object>>>();
        List<Map<Object, Object>> list  = checkLineService.getQualityAnalysisList(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }

}
